package com.suning.service;

import com.suning.constant.Constants;
import com.suning.entity.EsBasePojo;
import com.suning.es.EsConfig;
import com.suning.utils.*;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchScroll;
import io.searchbox.params.Parameters;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;
import net.sf.json.util.PropertySetStrategy;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.suning.constant.Constants.MAX_LIST_CAPACITY;
import static com.suning.constant.Constants.SCROLL_TIMEOUT;
import static io.searchbox.params.SearchType.DFS_QUERY_THEN_FETCH;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品 /模块版本] （可选）
 */
@Service
public class BaseService  {

    protected static Logger logger = LoggerFactory.getLogger(BaseService.class);

    /**
     * 封装对象返回
     *
     * @param <T>    the type parameter
     * @param clazz  最终转换的对象。
     * @return list 返回封装好的数据结构。
     * @throws Exception the exception
     */
    protected  <T extends EsBasePojo> void packageObjectPojo(long total,Class<T> clazz, List<T> result, JestResult scrollResult) {
        JSONArray hitsArray = JSONObject.fromObject(scrollResult.getJsonString()).getJSONObject("hits").getJSONArray("hits");
        for(Object object : hitsArray){
            JSONObject innerJsonObject = (JSONObject) object;
            String _index = (String) innerJsonObject.get("_index");
            String _type = (String) innerJsonObject.get("_type");
            String _id = (String)innerJsonObject.get("_id");
            JSONObject _source = (JSONObject) innerJsonObject.get("_source");
            JSONObject highlight = (JSONObject) innerJsonObject.get("highlight");
            JSONArray sort = (JSONArray) innerJsonObject.get("sort");
            //JSONObject _score = (JSONObject) innerJsonObject.get("_score");
            _source.put("_source", _source);
            _source.put("_index", _index);
            _source.put("_type",_type);
            _source.put("_id", _id);
            _source.put("highlight", highlight);
            _source.put("sort", sort);
            _source.put("total", total);
            JsonConfig config = new JsonConfig();
            //对时间进行自定义
            config.setRootClass(clazz);
            result.add((T) JSONObject.toBean(_source,config));
        }
    }
}
